import React, { useEffect, useState } from 'react'
import { Card, Table, Button, message } from 'antd';
import { getall, delone } from "../../../http/combo"
import { useDispatch } from "umi"
import UPdate from "../../../components/combouse"

export default function index() {
    const dispatch = useDispatch()
    const columns = [
        {
            title: '套餐名字',
            key: 'name',
            dataIndex: 'name'
        },
        {
            title: '套餐月数',
            dataIndex: 'mouth',
            key: 'mouth',
        },
        {
            title: '套餐折扣',
            dataIndex: 'salePrice',
            key: 'salePrice',
        },
        {
            title: '原价',
            key: 'normalPrice',
            dataIndex: 'normalPrice'
        },
        {
            title: '是否推荐',
            key: 'type',
            render: (text) => (
                <>
                    <span>{text ? "是" : '否'}</span>
                </>
            ),
        },

        {
            title: '操作',
            key: 'action',
            render: (text, data) => (
                <>
                    <Button onClick={() => update(text)} style={{ marginRight: '20px' }} >修改</Button>
                    <Button onClick={() => del(text)} type="primary">删除</Button>
                </>
            ),
        },
    ];
    const [data, setData] = useState(null)
    useEffect(() => {
        getuserData()
    }, [])


    //获取用户数据
    const getuserData = async () => {
        const res = await getall()
        if (res.meta.status == 200) {
            setData(
                res.data
            )
        }
    }

    //删除套餐
    const del = async (val) => {
        const res = await delone({
            _id: val._id
        })
        if (res.meta.status == 200) {
            message.success('删除成功')
            getuserData();
        } else {
            message.error('删除失败')
        }
    }
    //新增套餐
    const add = () => {
        dispatch({
            type: 'comboModels/changestatus',
            payload: true
        })
        dispatch({
            type: 'comboModels/changeopen',
            payload: '新增套餐'
        })
    }
    //修改套餐 
    const update = (val) => {
        dispatch({
            type: 'comboModels/changestatus',
            payload: true
        })
        dispatch({
            type: 'comboModels/changeopen',
            payload: '修改套餐'
        })
        dispatch({
            type: 'comboModels/addid',
            payload: val
        })
    }
    return (
        <>
            <Card title="套餐管理列表" extra={<a onClick={add}>新增</a>} style={{ width: "98.5%" }}>
                <Table columns={columns} rowKey="_id" dataSource={data} pagination={{ defaultCurrent: 1, defaultPageSize: 5, showQuickJumper: true }} />
            </Card>
            <UPdate getdata={getuserData}></UPdate>
        </>
    )
}
